package org.example.leetcpde;

import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;

import java.util.Arrays;

public class threeSumClosest implements IArray {
    @Test
    void aa() {
        int[] nums = parse("[13,252,-87,-431,-148,387,-290,572,-311,-721,222,673,538,919,483,-128,-518,7,-36,-840,233,-184,-541,522,-162,127,-935,-397,761,903,-217,543,906,-503,-826,-342,599,-726,960,-235,436,-91,-511,-793,-658,-143,-524,-609,-728,-734,273,-19,-10,630,-294,-453,149,-581,-405,984,154,-968,623,-631,384,-825,308,779,-7,617,221,394,151,-282,472,332,-5,-509,611,-116,113,672,-497,-182,307,-592,925,766,-62,237,-8,789,318,-314,-792,-632,-781,375,939,-304,-149,544,-742,663,484,802,616,501,-269,-458,-763,-950,-390,-816,683,-219,381,478,-129,602,-931,128,502,508,-565,-243,-695,-943,-987,-692,346,-13,-225,-740,-441,-112,658,855,-531,542,839,795,-664,404,-844,-164,-709,167,953,-941,-848,211,-75,792,-208,569,-647,-714,-76,-603,-852,-665,-897,-627,123,-177,-35,-519,-241,-711,-74,420,-2,-101,715,708,256,-307,466,-602,-636,990,857,70,590,-4,610,-151,196,-981,385,-689,-617,827,360,-959,-289,620,933,-522,597,-667,-882,524,181,-854,275,-600,453,-942,134]");
        System.out.println(threeSumClosest(nums, -2805));
    }
    public int threeSumClosest(int[] nums,
                               int target) {
        Arrays.sort(nums);
        int res = Integer.MAX_VALUE >> 1;

        int len = nums.length;

        for (int i = 0; i < len - 2; i++) {
            int v1 = nums[i];
            if (i > 0 && v1 == nums[i - 1]) {
                continue;
            }

            for (int j = i + 1, k = len - 1; j < k; ) {
                int v2 = nums[j];
                if (j > i + 1 && v2 == nums[j - 1]) {
                    continue;
                }

                int v3 = nums[k];
                int sum = v1 + v2 + v3;
                int diff = sum - target;

                if (diff == 0) {
                    return target;
                }

                if (diff < 0) {
                    j++;
                } else {
                    k--;
                }

                if (Math.abs(diff) < Math.abs(res - target)) {
                    res = sum;
                }
            }
        }

        return res;
    }

    @Nested
    class Solution {
        @Test
        void aa() {

        }

        public int threeSumClosest(int[] nums,
                                   int target) {
            Arrays.sort(nums);
            int n = nums.length;
            int best = 10000000;

            // 枚举 a
            for (int i = 0; i < n; ++i) {
                // 保证和上一次枚举的元素不相等
                if (i > 0 && nums[i] == nums[i - 1]) {
                    continue;
                }
                // 使用双指针枚举 b 和 c
                int j = i + 1, k = n - 1;
                while (j < k) {
                    int sum = nums[i] + nums[j] + nums[k];
                    // 如果和为 target 直接返回答案
                    if (sum == target) {
                        return target;
                    }
                    // 根据差值的绝对值来更新答案
                    if (Math.abs(sum - target) < Math.abs(best - target)) {
                        best = sum;
                    }
                    if (sum > target) {
                        // 如果和大于 target，移动 c 对应的指针
                        int k0 = k - 1;
                        // 移动到下一个不相等的元素
                        while (j < k0 && nums[k0] == nums[k]) {
                            --k0;
                        }
                        k = k0;
                    } else {
                        // 如果和小于 target，移动 b 对应的指针
                        int j0 = j + 1;
                        // 移动到下一个不相等的元素
                        while (j0 < k && nums[j0] == nums[j]) {
                            ++j0;
                        }
                        j = j0;
                    }
                }
            }
            return best;
        }
    }
}
